<?php ob_start();
session_start(); // on relaye la session 
if(isset($_SESSION["is_registered"]) && $_SESSION["is_registered"] == "registered"){
		$enregistrer = $_SESSION["is_registered"];
}else{
	header("location:../../../menu.php"); // redirection en cas d'echec
}
require_once('../../../Connections/microassur.php');
require_once('../../../utils/fonctions.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$currentPage = $_SERVER["PHP_SELF"];

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if (isset($_POST["MM_insert"]))
{
	if(isset($_POST['Code_garantie']) && ($_POST['Code_garantie'])!=NULL)
		{
			if(veriflibelle1("Code_garantie"))
			{
				 $toCriterias['Code_garantie'] = $_POST['Code_garantie'] ;
                 $toCriterias['Identifiant_produit'] = $_POST['Identifiant_produit'] ;
                 $nums=existeEnregistrement($database_microassur, $microassur,"garantie",$toCriterias);
				 // $nums=existeChamp($database_microassur, $microassur,"garantie","Code_garantie", $_POST['Code_garantie']);
					if($nums>=intval(1))
						{
						$message = colorer("Cette garantie existe déjà pour ce prduit","false");
						}
					else
						{
							$veriflibelle=verifLibelle("Libelle_garantie");
								if($veriflibelle==TRUE)
								{
									if($_POST['Type_garantie']!=NULL && $_POST['Identifiant_produit']!=NULL )
									{
											if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) 
											{
											  $insertSQL = sprintf("INSERT INTO garantie (Identifiant_garantie, Code_garantie, Libelle_garantie, Type_garantie, Identifiant_produit) VALUES (%s, %s, %s, %s, %s)",
																   GetSQLValueString($_POST['Identifiant_garantie'], "int"),
																   GetSQLValueString($_POST['Code_garantie'], "text"),
																   GetSQLValueString($_POST['Libelle_garantie'], "text"),
																   GetSQLValueString($_POST['Type_garantie'], "text"),
																   GetSQLValueString($_POST['Identifiant_produit'], "int"));

											  mysql_select_db($database_microassur, $microassur);
												 mysql_query("START TRANSACTION");
											  $Result1 = mysql_query($insertSQL, $microassur) or die(mysql_error());
														if($Result1==intval(1))
														{	
															mysql_query("commit");
															$message = colorer("La garantie est créée ","true");
															unset($_POST);
														}
														else
															{mysql_query("rollback");$message = colorer("Erreur: Contacter la DSI","false");}
											}
									}
									else
										$message="";
								}
								else
									$message = colorer("Vous devez saisir le libellé","false");
						}
			}
			else
				$message = colorer("Le code saisi comporte des caractères spéciaux interdits","false");
		}
		else
			$message = colorer("Vous devez saisir le code","false");
}

$maxRows_rs = 10;
$pageNum_rs = 0;
if (isset($_GET['pageNum_rs'])) {
  $pageNum_rs = $_GET['pageNum_rs'];
}
$startRow_rs = $pageNum_rs * $maxRows_rs;

mysql_select_db($database_microassur, $microassur);
$query_rs = "SELECT garantie.*,produit.identifiant_produit,produit.libelle_produit FROM garantie,produit WHERE garantie.Identifiant_produit=produit.Identifiant_produit";
//$query_limit_rs = sprintf("%s LIMIT %d, %d", $query_rs, $startRow_rs, $maxRows_rs);
$query_limit_rs = $query_rs;
$rs = mysql_query($query_limit_rs, $microassur) or die(mysql_error());
$row_rs = mysql_fetch_assoc($rs);

if (isset($_GET['totalRows_rs'])) {
  $totalRows_rs = $_GET['totalRows_rs'];
} else {
  $all_rs = mysql_query($query_rs);
  $totalRows_rs = mysql_num_rows($all_rs);
}
$totalPages_rs = ceil($totalRows_rs/$maxRows_rs)-1;

mysql_select_db($database_microassur, $microassur);
$query_rs_prod = "SELECT Identifiant_produit, Libelle_produit FROM produit";
$rs_prod = mysql_query($query_rs_prod, $microassur) or die(mysql_error());
$row_rs_prod = mysql_fetch_assoc($rs_prod);
$totalRows_rs_prod = mysql_num_rows($rs_prod);
/**************************************************************************************/
$query_rs_dico1 = 'SELECT Identifiant_table FROM dictionnaire where Code_table="T0001"';
$rs_dico1 = mysql_query($query_rs_dico1, $microassur) or die(mysql_error());
$row_rs_dico1 = mysql_fetch_assoc($rs_dico1);
$num_row = $row_rs_dico1['Identifiant_table'];
/**************************************************************************************/
$query_rs_dico2 = "SELECT Code_element, Nom_element FROM elementdico where Identifiant_table = '$num_row'";
$rs_dico2 = mysql_query($query_rs_dico2, $microassur) or die(mysql_error());
$row_rs_dico2 = mysql_fetch_assoc($rs_dico2);

$queryString_rs = "";
if (!empty($_SERVER['QUERY_STRING'])) {
  $params = explode("&", $_SERVER['QUERY_STRING']);
  $newParams = array();
  foreach ($params as $param) {
    if (stristr($param, "pageNum_rs") == false && 
        stristr($param, "totalRows_rs") == false) {
      array_push($newParams, $param);
    }
  }
  if (count($newParams) != 0) {
    $queryString_rs = "&" . htmlentities(implode("&", $newParams));
  }
}
$queryString_rs = sprintf("&totalRows_rs=%d%s", $totalRows_rs, $queryString_rs);
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<meta http-equiv="X-UA-Compatible" content="IE=8" />
			<title><?php echo $titleGen ?></title>
			<link rel="stylesheet" type="text/css" href="../../../css/main.css" />
			<link rel="stylesheet" type="text/css" href="../offre.css" />
			<!--[if IE 7]><link rel="stylesheet" type="text/css" href="../../../css/ie7.css" /><![endif]-->
	</head>
<body>
			<div id="sf_menu1">
				<h3>Administration</h3>
				<?php
					if(isset($message))
						echo $message;
					$nommodule="Garantie";
					$nommodule2="Liste des garanties";
					$nom_module_saisie="Ecran saisie";
					?>
			</div>
			<div class="cleared"></div>
		<div class="corps1">
			<!--/***************************************/-->
			<div class="imgleft"></div>
			<div class="imgmiddle">
			<div class="btn"><span class="btnleft"></span><span class="btnmiddle"><h5><?php echo $nommodule; ?></h5></span><span class="btnright"></span></div>
			<div class="btn1"><span class="btnleft"></span><span class="btnmiddle"><h5><?php echo $nommodule2; ?></h5></span><span class="btnright"></span></div>
			<div class="cleared"></div>
			
			<table class="tab_list">
						  <tr id="niv1">
							<td class="cod_gar">CODE</td>
							<td class="lib_gar">LIBELLE</td>
							<td class="type_gar">TYPE</td>
							<td class="prod_gar">PRODUIT</td>
						  </tr>
			</table>
			
			
			
				<div class="content_table">	
				           <table class="tab_list ns0">
						  <?php $i=1; do { ?>
							  <?php if ($totalRows_rs > 0) { // Show if recordset not empty ?>
							  <tr <?php if($i%2==0) echo 'class="colorligne"'; ?> >
								<td class="cod_gar"><a href="modgarantie.php?id=<?php echo $row_rs['Identifiant_garantie']; ?>"><?php echo $row_rs['Code_garantie']; ?></a></td>
								<td class="lib_gar"><a href="modgarantie.php?id=<?php echo $row_rs['Identifiant_garantie']; ?>"><?php echo $row_rs['Libelle_garantie']; ?></a></td>
								<td class="type_gar"><a href="modgarantie.php?id=<?php echo $row_rs['Identifiant_garantie']; ?>"><?php if($row_rs['Type_garantie']=="B") echo "Obligatoire"; else if($row_rs['Type_garantie']=="P") echo "Optionnelle"; ?></a></td>
								<td class="prod_gar"><a href="modgarantie.php?id=<?php echo $row_rs['Identifiant_garantie']; ?>"><?php echo $row_rs['libelle_produit']; ?></a></td>
							  </tr>
								<?php } // Show if recordset not empty ?>

							  <?php $i++; } while ($row_rs = mysql_fetch_assoc($rs)); ?>
									</table>
								</div>
							</div>
							<div class="imgright"></div>
						<div class="cleared"></div>
						<!--/***************************************/-->
						<div id="br"></div>
						
			<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
							<div class="imgleft"></div>
							<div class="imgmiddle">
								<div class="btn"><span class="btnleft"></span><span class="btnmiddle"><h5><?php echo $nommodule; ?></h5></span><span class="btnright"></span></div>
								<div class="btn1"><span class="btnleft"></span><span class="btnmiddle"><h5><?php echo $nom_module_saisie; ?></h5></span><span class="btnright"></span></div>
								<div class="cleared"></div>
									<div class="dikk">
										  <table>
											<tr>
											  <td>CODE</td>
											  <td><input class="resize_1" type="text" name="Code_garantie" value="<?php echo remind_text("Code_garantie"); ?>" size="15" MAXLENGTH="5" /></td>
											</tr>
											<tr>
											  <td>LIBELLE</td>
											  <td><input type="text" name="Libelle_garantie"  maxlength="30" value="<?php echo remind_text("Libelle_garantie"); ?>" size="50" /></td>
											</tr>
											<tr>
											  <td>TYPE</td>
											  <td><select name="Type_garantie">
										 <?php do { ?>
												<option value="<?php echo $row_rs_dico2['Code_element']; ?>" <?php echo remind_select('Type_garantie', $row_rs_dico2['Code_element']) ?> ><?php echo $row_rs_dico2['Nom_element']; ?></option>
										<?php } while($row_rs_dico2 = mysql_fetch_assoc($rs_dico2)); ?>
											  </select>
											  </td>
											</tr>
											<tr>
											  <td>PRODUIT</td>
											  <td><select name="Identifiant_produit">
												<?php 
										do {
										?>
												<option value="<?php echo $row_rs_prod['Identifiant_produit']?>" ><?php echo $row_rs_prod['Libelle_produit']?></option>
												<?php
										} while ($row_rs_prod = mysql_fetch_assoc($rs_prod));
										?>
											  </select>
											  </td>
											</tr>
										  </table>
									</div>
							</div>
							<div class="imgright"></div>				
								<div class="cleared"></div>
							
									<div class="bt1"><a href="<?php echo $retour; ?>"><span class="btleft"></span><span class="btmiddle">Retour</span><span class="btright"></span></a></div>
									<div class="bt"><span class="btleft"></span><input type="submit" name="Creer" value="Créer" class="resizeall btmiddle"/><span class="btright"></span></div>
								<div class="cleared"></div>
			  <input type="hidden" name="Identifiant_garantie" value="" />
			  <input type="hidden" name="MM_insert" value="form1" />
			</form>
</div> 
</body>
</html><?php ob_end_flush();?>
<?php
mysql_free_result($rs);

mysql_free_result($rs_prod);
?>